// pages/store/store.js
import { requestLoading, request } from '../../utils/network.js'
var app = getApp()
var globalData = app.globalData
Page({

  /**
   * 页面的初始数据
   */
  data: {
    storeList:[
      // {
      //   supplierId: 1,
      //   shopName: '钻头点',
      //   productsSum: 12
      // },
      // {
      //   supplierId: 2,
      //   shopName: '钻头点',
      //   productsSum: 12
      // },
      // {
      //   supplierId: 3,
      //   shopName: '钻头点',
      //   productsSum: 12
      // },
      // {
      //   supplierId: 5,
      //   shopName: '钻头点',
      //   productsSum: 12
      // }
    ],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this
    let token = wx.getStorage({
      key: 'token',
      success: function (res) {
        that.fetchBill()
      },
      fail: () => {
        wx.navigateTo({
          url: '/pages/login/login',
        })
      }
    })
    
  },
  /**
 * 页面相关事件处理函数--监听用户下拉动作
 */
  onPullDownRefresh: function () {
    let that = this
    that.fetchBill()
  },
  /**
   * 领取所有配货单
   */
  receiveAll: (e) => {
    let that = this
    let suppid = e.target.dataset.suppid
    request(
     '/distributor/claimedBillBySupplierId',
     { supplierId: suppid},
     function success (data) {
       let code = data.resultCode
       if (code === 2000) {
         that.fetchBill()
         wx.showToast({
           title: '领取成功',
           icon: 'success',
           duration: 1000
         })
       }
     },
     function fail () {
       wx.showToast({
         title: '网络连接失败',
         icon: 'none',
         duration: 1000
       })
     }
    )
  },
  /**
   * 转发页面信息
   */
  onShareAppMessage(){
    var that = this
    return {
      title: '刀具联盟-配货端',
      imageUrl: '../../images/knife_logo.png',
      success: function (res){
        if (res.errMsg === 'shareAppMessage:ok'){

        }
      },
      fail: function () {
　　　　if (res.errMsg == 'shareAppMessage:fail cancel') {
　　　　　// 用户取消转发
　　　　} else if (res.errMsg == 'shareAppMessage:fail') {
　　　　　// 转发失败，其中 detail message 为详细失败信息
　　　　}
　　　}
  }
  },
  /**
   * 获取数据
   */
  fetchBill () {
    let that = this
    requestLoading(
      '/distributor/unclaimedShopList',
      '',
      '加载供应商',
      function success(data) {
        that.setData({
          storeList: data.data
        })
      },
      function fail() {
        wx.showToast({
          title: '网络连接失败',
          icon: 'none',
          duration: 1000
        })
      }
    )
  }
})